package business.playgameForm_Evt;

import java.io.File;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;

public class PlaySound implements Runnable{
	
	public Clip clip;
	private final File wordFound = new File("src/Sounds/WordFound.wav");
    private final File wordNotFound = new File("src/Sounds/NotFound.wav");
    private final File clockTicking = new File("src/Sounds/Clock-Ticking.wav");
    private final File mousePreesed = new File("src/Sounds/Mouse_pressed.wav");
    private final File mouseOver = new File("src/Sounds/Mouse_over.wav");
    private final File nextLevel = new File("src/Sounds/NextLevel.wav");
    private final File highScore = new File("src/Sounds/Luckybox.wav");
    
    public static final int WORD_FOUND = 0;
    public static final int WORD_NOT_FOUND = 1;
    public static final int CLOCK_TICKING = 2;
    public static final int MOUSE_PRESSED = 3;
    public static final int MOUSE_OVER = 4;
    public static final int NEXT_LEVEL = 5;
    public static final int HIGH_SCORE = 6;
    
    public int soundMode = 0;
	
	/**
     * Play word found sound
     */
    public void wordFoundSound() {
            playSound(wordFound);
    }

    /**
     * Play word not found sound
     */
    public void wordNotFoundSound() {
            playSound(wordNotFound);
    }

    /**
     * Play clock-ticking sound when your time is equals 10 seconds
     */
    public void clockTickingSound() {
            playSound(clockTicking);
    }
    
    public void mousePressed()
    {
            playSound(mousePreesed);
    }
    
    public void mouseOver()
    {
            playSound(mouseOver);
    }
    
    public void nextLevel()
    {
            playSound(nextLevel);
    }
    
    public void highScore()
    {
    	playSound(highScore);
    }

    /**
     * this method will play your *.wav sound with your input sound file
     *
     * @param soundFile
     */
    private void playSound(File soundFile) {
            try {
                    Line.Info linfo = new Line.Info(Clip.class);
                    Line line = AudioSystem.getLine(linfo);
                    clip = (Clip) line;
                    clip.addLineListener(listener);
                    AudioInputStream audioStreamInput = AudioSystem.getAudioInputStream(soundFile);
                    clip.open(audioStreamInput);
                    clip.start();
            } catch (Exception e) {
            }
    }
    private LineListener listener = new LineListener() {

            @Override
            public void update(LineEvent event) {
                    LineEvent.Type type = event.getType();

                    if (type == LineEvent.Type.STOP) {
                            clip.close();
                    }
            }
    };

	@Override
	public void run() 
	{
		try
		{
			switch(soundMode)
			{
				case WORD_FOUND:{wordFoundSound();}break;
				case WORD_NOT_FOUND:{wordNotFoundSound();}break;
				case CLOCK_TICKING:{clockTickingSound();}break;
				case MOUSE_PRESSED:{mousePressed();}break;
				case MOUSE_OVER:{mouseOver();}break;
				case NEXT_LEVEL:{nextLevel();}break;
				case HIGH_SCORE:{highScore();}break;
			}
		}catch(Exception ex)
		{
			
		}
	}
}
